package triannon.client.sprites;

import java.awt.image.BufferedImage;
import java.util.Iterator;

import org.jdom.Element;

import triannon.client.interfaces.IXML;
import triannon.client.sprites.renderer.IRenderer;
import triannon.client.sprites.renderer.ImageRenderer;
import triannon.client.util.ResourceLocator;
import triannon.client.util.XMLIO;

public class Animation implements IXML {
	private AnimationKey key;
	private int frameCount;
	private IRenderer[] frames;
	private final String spriteKey;
	private final SpriteType spriteType;
	private boolean defaultAnimation = false;
	private int imageWidth;
	private int imageHeight;
	private int i = 0;
	private float animationSpeed = 0f;

	public Animation(SpriteType spriteType, String spriteKey, Element element) {
		this.spriteType = spriteType;
		this.spriteKey = spriteKey;
		parseXML(element);
	}

	public AnimationKey getKey() {
		return key;
	}

	public IRenderer getFrame(int i) {
		return frames[i % frameCount];
	}

	public IRenderer getStartFrame() {
		return frames[0];
	}

	public IRenderer getNextFrame() {
		i = (i + 1) % frameCount;
		return frames[i];
	}

	public IRenderer incFrame(int inc) {
		i = (i + inc) % frameCount;
		return frames[i];
	}

	public void parseXML(Element element) {
		String s;

		s = element.getAttributeValue("key");
		key = AnimationKey.valueOf(s.toUpperCase());

		s = element.getAttributeValue("framecount");
		frameCount = Integer.valueOf(s);
		frames = new ImageRenderer[frameCount];

		s = element.getAttributeValue("default");
		if (s != null) {
			defaultAnimation = Boolean.valueOf(s);
		}

		s = element.getAttributeValue("speed");
		if (s != null) {
			animationSpeed = Float.valueOf(s);
		}

		Iterator<?> it = element.getChildren("frame").iterator();

		int i;
		while (it.hasNext()) {
			Element frame = (Element) it.next();

			s = frame.getAttributeValue("key");
			i = Integer.valueOf(s);

			s = frame.getAttributeValue("file");
			BufferedImage bi = ResourceLocator.getSpriteImage(spriteType, spriteKey,
					s);
			imageWidth = bi.getWidth();
			imageHeight = bi.getHeight();
			frames[i] = new ImageRenderer(bi);
		}
	}

	public boolean isDefaultAnimation() {
		return defaultAnimation;
	}

	public int getImageWidth() {
		return imageWidth;
	}

	public int getImageHeight() {
		return imageHeight;
	}

	public float getAnimationSpeed() {
		return animationSpeed;
	}

	@Override
	public Element toXML() {
		throw new UnsupportedOperationException("Xml output not supported");
	}

	public static void main(String[] args) {
		ResourceLocator.addResourceDir("triannon/client/resource/sprites/");
		Element rootElem = XMLIO.readXML(ResourceLocator.getResource("unit.xml"));

		Element anim = rootElem.getChild("sprite").getChild("animation");

		Animation info = new Animation(SpriteType.UNIT, "grunt", anim);

		System.err.println(info.getKey());
		System.err.println(info.getFrame(0));
	}
}
